//
//  V2MenuCombCell.m
//  BaseProject
//
//  Created by 王洋 on 2021/4/26.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "V2MenuCombCell.h"

@interface V2MenuCombCell ()<UITableViewDelegate, UITableViewDataSource>



@end

@implementation V2MenuCombCell

- (void)awakeFromNib {
    [super awakeFromNib];


    _tableview.scrollEnabled = NO;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableview.showsHorizontalScrollIndicator = NO;
    _tableview.showsVerticalScrollIndicator = NO;
    
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    _tableview.estimatedRowHeight = 0;
    
    _tableview.backgroundColor =  UIColorHex(0xf1f1f1);
    _tableview.showsVerticalScrollIndicator = NO;
    
    [_tableview registerNib:[UINib nibWithNibName:@"V2MenuCombCell" bundle:nil] forCellReuseIdentifier:@"V2MenuCombCellID"];
    [_tableview registerNib:[UINib nibWithNibName:@"V2MenuSingleCell" bundle:nil] forCellReuseIdentifier:@"V2MenuSingleCellID"];

}

- (void)fillWithModel:(V2CommonMenuModel *)model status:(V2MenuSingleCellStatus)status {

    self.status = status;
    self.dataArray = @[].mutableCopy;
    self.dataArray = model.dishesList.mutableCopy;
    
    
    NSString *nameStr = [NSString stringWithFormat:@"·%@",model.dishesName];
    
    NSNumberFormatter  *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
    NSString *ch_string = [formatter stringFromNumber:[NSNumber numberWithLong:model.dishesList.count]];
    
    NSString *countStr = [NSString stringWithFormat:@"（%@选一）",ch_string];
    NSString *str = [NSString stringWithFormat:@"%@%@",nameStr,countStr];
    
    if (status == V2MenuSingleCellStatus_edit) {
                
        //富文本
        NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:str];
        NSRange nameRange = NSMakeRange(0, nameStr.length);
        NSRange countRange = NSMakeRange(nameStr.length,countStr.length);
        // 设置字体大小
        [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium] range:nameRange];
        [attrStr addAttribute:NSForegroundColorAttributeName value:UIColorHex(0x333333) range:nameRange];
        
        [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:countRange];
        [attrStr addAttribute:NSForegroundColorAttributeName value:UIColorHex(0xff7200) range:countRange];

        self.titleLab.attributedText = attrStr;
        
    } else {
        self.titleLab.text = str;
    }
    
    [self.tableview reloadData];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    V2MenuSingleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"V2MenuSingleCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    cell.bgView.hidden = NO;
    [cell fillWithModel:self.dataArray[indexPath.row] status:self.status];
    
    WEAKSELF
    cell.selectSingleCellBlock = ^{
        if (weakSelf.V2MenuCombCellBlock) {
            weakSelf.V2MenuCombCellBlock(indexPath.row);
        }
    };
    
    return cell;
}



@end
